package nciae.db.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import nciae.db.common.CommonResult;
import nciae.db.pojo.dto.*;
import nciae.db.pojo.vo.ApiGetDateLogVO;
import nciae.db.pojo.vo.LogVo;
import nciae.db.pojo.vo.LogVoId;
import nciae.db.service.ILogService;
import nciae.db.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.support.ResourceTransactionManager;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

//日志记录


@Controller
@CrossOrigin
public class LogController {

    @Autowired
    private ILogService logService;

    @Autowired
    private ResourceTransactionManager resourceTransactionManager;

    //  页面返回
    @GetMapping("/log/index")
    public String index(){
        return "log/index";
    }

    //  form表单
    @GetMapping("/log/form")
    public ModelAndView form(Long id, Model model){
        LogVoId log;
        log = logService.logInfo(id);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("log/form.html");
        model.addAttribute("log",log);
        if(log!=null){
            return mav;
        }
        else {
            return null;
        }
    }
    //   表单的数据回显
    @ResponseBody
    @RequestMapping("/log/infoId")
    public CommonResult<LogVoId> infoId(@RequestParam Long id){
        LogVoId logVo = logService.logInfo(id);
        CommonResult<LogVoId> commonResult = new CommonResult<>();
        commonResult.setData(logVo);
        return commonResult;
    }

    // 成绩的评阅
    @ResponseBody
    @PostMapping("/log/save")
    public CommonResult save(@RequestBody LogDato log, HttpSession session){
        return logService.logsave(log,session);
    }

    // 信息的删除
    @ResponseBody
    @RequestMapping("/log/del")
    public CommonResult del(@RequestParam Long id){
        return logService.logDelInfo(id);
    }


    @ResponseBody
    @PostMapping("/api/logInsert")
    public CommonResult<Boolean> ApilogInsert(@RequestBody ApiLogDTO apiLogDTO){
        CommonResult<Boolean> result = new CommonResult<>();
        result = logService.ApilogInsert(apiLogDTO);
        return result;
    }


    // 综合查询2
    @ResponseBody
    @RequestMapping("/log/infoLogAll")
    public CommonResult<List<LogVo>> infoLogAll(int page, int limit, Integer state, String logDate, String project,String classs,String name, HttpSession session){

        return logService.searchInfoAll(page, limit, state, logDate,project,classs,name,session);
    }


    //文件的上传
    @PostMapping("/api/UploadDairyFiles")
    @ResponseBody
    public CommonResult<String> UploadHomeWorkFiles(
            @RequestParam("file") MultipartFile file,
            @RequestParam("projectId") Long projectId,
            @RequestParam("userId") Long userId,
            @RequestParam("date") String date) throws IOException {
        //文件上传并将路径存入数据库
        System.out.println(file.getOriginalFilename());
        System.out.println(projectId);
        System.out.println(date);
        return logService.UploadDiaryFiles(file,projectId,userId,date);

    }

    //文件的删除
    @PostMapping("/api/deleteDiaryFiles")
    @ResponseBody
    public CommonResult<String> deleteDiaryFiles(@RequestBody ApiDeleteDiaryFileDTO apiDeleteDiaryFileDTO) throws IOException {
        System.out.println("sssssssssssssssssssoooooooooooooo");
        System.out.println(apiDeleteDiaryFileDTO);
        return FileUploadUtils.deleteDiaryFile(apiDeleteDiaryFileDTO);
    }

    //文件的下载
    @ResponseBody
    @GetMapping("/log/downloadFiles")
    public ResponseEntity<Resource> downloadFiles(Integer id){
        return logService.downloadFiles(id);
    }


    /**/

}








